<template>
  <div class="content">
<!--  标题栏  -->
      <a-affix :offset-top="top">
          <headers></headers>
      </a-affix>

<!--  轮播图  -->
    <a-carousel autoplay>
      <div>
          <img src="@/assets/img/overview/007.jpeg" alt="">
      </div>
      <div>
          <img src="@/assets/img/overview/008.jpeg" alt="">
      </div>
        <div>
            <img src="@/assets/img/overview/004.jpeg" alt="">
        </div>
    </a-carousel>
<!--  商品分区    -->
      <div class="goods">
            <div v-for="item in data">
                <div class="title" >
                    <div class="leftTitle"><span style="color: #ff4d4f">{{item.name}}</span>-{{item.typeName}}</div>
                    <div class="rightTitle">更多</div>
                </div>
                <!--  需要判断长度是否为0， 为0则进行页面提示 -->
                <div class="empty" v-if="!item.goodsEntityList.length">
                    <a-empty />
                </div>
                <div v-else class="foods"  >
                    <div class="list" v-for="item2 in item.goodsEntityList">
                        <div class="topContent">
                            <div class="topImg">
                                <!--                            <img :src="item2.goodsPicture" alt="">-->
                                <a-image height="100%"
                                         :preview="false"
                                         @click="toGoodsDetail(item2)"
                                         :src="item2.goodsPicture"
                                         fallback="">
                                </a-image>
                            </div>
                        </div>
                        <div class="bottomText">
                            <div class="price">
                                <div>
                                    <span>{{item2.goodsName}}</span>
                                </div>
                                <div>
                                    <span style="color: red">￥{{item2.presentPrice}}</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
      </div>
  </div>
</template>

<script>
import {defineComponent, onMounted, ref, reactive, toRefs} from "vue";
import headers from "./components/header";
import {apiGoodsAllList, getImgUrl, apiGetGoodsDetail} from "../../api";
import {useRouter} from "vue-router";

export default defineComponent({
  name: "index",
  components: { headers},
    setup() {
      const router = useRouter()
        const state = reactive({
            data: []
        })
      // 获取所有商品列表
        const getList = () => {
            apiGoodsAllList({}).then((res) => {
                console.log(res.data)
                state.data = res.data.data
                state.data.map((item) => {
                    if (item.goodsEntityList.length != 0) {
                        item.goodsEntityList.map(async (item2) => {
                            const params = {
                                filename: item2.goodsPicture
                            }
                            const imgData = await getImgUrl(params)
                            const imageUrl = `data: image/jpeg;base64,${btoa(new Uint8Array(imgData.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`
                            item2.goodsPicture = imageUrl
                            return item2
                        })
                        // console.log('图片为微微',item)
                        return item
                    }
                })
            })
        }
        // 点击图片，进入商品详情
        const toGoodsDetail = (val) => {
            console.log(99999, val)
            // 将图片地址存到session中
            window.sessionStorage.setItem('imgUrl', val.goodsPicture)
            apiGetGoodsDetail({id: val.id}).then((res) => {
                console.log(88888, res)
                if (res.data.code == 200 ) {
                    const data = res.data.data
                    // 已经登录，跳转到详情页
                    router.push({name: 'goodsDetail', params: {detail: JSON.stringify(data)}})
                }
                // return
            })
        }

        // const getImgSrc = async (val) => {
        //     const params = {
        //         filename: val
        //     }
        //     const imgData = await getImgUrl(params)
        //     const imageUrl = `data: image/jpeg;base64,${btoa(new Uint8Array(imgData.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`
        //     console.log('图片为', imageUrl)
        //     return imageUrl
        // }
      onMounted(() => {
          getList()
      })
      return {
          ...toRefs(state),
          toGoodsDetail
      }
    }
})
</script>

<style lang="less" scoped>
.ant-carousel :deep(.slick-slide) {
  text-align: center;
    width: 100%;
    height: 300px;
    line-height: 160px;
    background: #364d79;
    overflow: hidden;
      img{
          width: 100%;
      }
}

.ant-carousel :deep(.slick-slide h3) {
  color: #fff;
}
.content {
    width: 100%;
    height: 100%;

    .goods {
        width: 80%;
        margin: 0 auto;
        height: 100%;
        background: aliceblue;
        //overflow-y: auto;

        .title {
            height: 35px;
            //background: #364d79;
            font-weight: bold;
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 20px;

            .leftTitle {
                padding-left: 15px;
            }
            .rightTitle {
                padding-right: 15px;
            }
        }

        .foods {
            //display: grid;
            //grid-template-columns:15% 15% 15% 15% 15%;/*3行，每列宽100px*/
            //grid-template-rows:200px 200px;/*3行，每行高200px*/
            //grid-row-gap:30px;
            //grid-column-gap:30px;
            //align-items: center;
            //justify-content: center;
            //background: rosybrown;

            display: flex;
            align-items: center;
            ////justify-content: space-between;
            ////justify-content: space-between;
            //justify-content: space-evenly;
            flex-wrap: wrap;

            .empty {
                width: 100%;
                height: 150px;
            }

            .list {
                width: 18%;
                height: 280px;
                margin-left: 20px;
                background: #fff;
                //margin-bottom: 15px;
                //height: 100%;

                .topContent {
                    height: 75%;
                    .topImg {
                        height: 100%;
                    }
                }

                .bottomText {
                    position: relative;
                    //bottom: 0;
                    height: 25%;
                    text-align: center;
                    //padding-top: 8px;

                    .price {
                        position: absolute;
                        bottom: 0;
                        left: 50%;
                        transform: translateX(-50%);
                        span {
                            font-size: 16px;
                        }
                    }

                }

            }
        }
    }
}
</style>
